<?php
namespace App\Components;
/**
 * Class Auth
 * @package App\Components
 */
class Auth{
    
    /**
     * 加入验证参数
     * @param unknown $data
     */
    public static function params($api_key,$data = []){
        $arr['time'] = time();  
        if ($data){
            $arr = array_merge($data,$arr);
        }
        $open_key = $api_key;
        ksort($arr);
        $str = '';
        foreach ($arr as $key=>$v){
            $str .= $key.$v;
        }
        $str .= $open_key;
        $md5_key = md5($str);
        $arr['sign'] = $md5_key;    
        return $arr;    
    }
    
    public static function check($api_key,$data){
        if (!isset($data['sign'])){
            return false;
        }
        $sign = $data['sign'];
        unset($data['sign']);
        $time = time() - $data['time'];
        $time = abs($time);
        if ($time > 600){
            return false;
        }
        $open_key = $api_key;
        ksort($data);
        $str = '';
        foreach ($data as $key=>$v){
            $str .= $key.$v;
        }
        $str .= $open_key;
        $md5_key = md5($str);
        if ($sign == $md5_key){
            return true;
        }else {
            return false;
        }
    }
    public static function curl($action, $params, $httpMethod='POST', $contentType='urlencoded'){
        return Request::curl($action, $params, $httpMethod='POST', $contentType='urlencoded');
    }
}